/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bakul;

import Tools.Koneksi;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gatz
 */
public class BakulKontrol {

    private Connection conn;

    public BakulKontrol(Connection koneksi) {
        this.conn = koneksi;
    }

    public static BakulKontrol getKoneksiBakul() throws SQLException {
        BakulKontrol kon = new BakulKontrol(Koneksi.bukaKoneksi());
        return kon;
    }

    public void tambahBakul(Bakul bakul) throws SQLException {
        String id = bakul.getIdBakul();
        String nama = bakul.getNamaBakul();
        String sql = "insert into Bakul (IdBakul, NamaBakul)"
                + "values ('" + id + "','" + nama + "')";

        Statement stat = conn.createStatement();
        stat.executeUpdate(sql);
        conn.close();
    }

    public void tambahBakulCall(Bakul bakul) throws SQLException {
        CallableStatement callst = null;
        String namanya = " ";
        try {
            conn.setAutoCommit(false);
            String sql = "{CALL spInsertBakul(?,?)}";
            callst = conn.prepareCall(sql);
            callst.setString(1, bakul.getIdBakul());
            callst.setString(2, bakul.getNamaBakul());
            callst.execute();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        }
    }

    public void updateBakul(Bakul bakul) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql = "update Bakul set NamaBakul=? where IdBakul=?";
            statement = conn.prepareStatement(sql);
            statement.setString(1, bakul.getNamaBakul());
            statement.setString(2, bakul.getIdBakul());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        }
    }

    public void deleteBakul(Bakul bakul) throws SQLException {
        String id = bakul.getIdBakul();
        String sql = "delete Bakul "
                + "where IdBakul='" + id + "'";
        Statement stat = conn.createStatement();
        stat.executeUpdate(sql);
        conn.close();
    }

    public List<Bakul> lihatSeluruhBakul() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;

        conn.setAutoCommit(false);
        statement = conn.prepareStatement("select IdBakul, NamaBakul from Bakul order by IdBakul");
        result = statement.executeQuery();
        List<Bakul> bakul = new ArrayList<Bakul>();

        while (result.next()) {
            Bakul bakul2 = new Bakul();
            bakul2.setIdBakul(result.getString("IdBakul"));
            bakul2.setNamaBakul(result.getString("NamaBakul"));
            bakul.add(bakul2);
        }
        conn.commit();
        return bakul;
    }
    
    public String carikodebakul() throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        long idbakul = 0;
        String a = "", fix = "";
        try {
            conn.setAutoCommit(false);
            String sql = "select max(idBakul) from bakul ";
            statement = conn.prepareStatement(sql);
            result = statement.executeQuery();
            while (result.next()) {
                a = result.getString("max(idbakul)");
                if (a == null) {
                    idbakul = 0;
                    break;
                }
                idbakul = Long.parseLong(a);
            }
            conn.commit();
        } catch (SQLException ex) {
            Logger.getLogger(BakulKontrol.class.getName()).log(Level.SEVERE, null, ex);
        }
        idbakul = idbakul + 1;
        String tmbh1 = String.valueOf(idbakul);
        if (idbakul < 10) {
            String a1 = "00";
            fix = a1 + tmbh1;
        } else if (idbakul < 100 && idbakul > 9) {
            String b = "0";
            fix = b + tmbh1;
        } else if (idbakul > 99 && idbakul < 1000) {
            fix = String.valueOf(idbakul);
        }
        return fix;
    }

}
